class ArticlesController < ApplicationController
  layout "home"
  
  # GET /articles
  # GET /articles.xml
  def index
    @filter = params[:filter].nil? ? {} : params[:filter]
    @page = params[:page].nil? ? nil : params[:page]
    @articles = Article.filter(@filter).paginate :page=>params[:page], :order=>'created_at desc', :per_page => 5
    # @articles = Article.all.paginate :page=>(params[:page].to_i - 1), :order=>'created_at desc', :per_page => 3 if @articles.count == 0 && !params[:page].nil?
    @users = User.all
    @article_statuses = ArticleStatus.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @articles }
      format.js { render 'articles.js.erb' }
    end
  end
  
  def home
    @articles = Article.find(:all, :order => "created_at", :limit => 7)
    @recent_posts = Article.find(:all, :order => "created_at", :limit => 5)
    @featured_posts = Article.find(:all, :order => "created_at", :limit => 5)
    @picture_posts = Article.find(:all, :order => "created_at", :limit => 5)
    @popular_posts = Article.find(:all, :order => "created_at", :limit => 5)
    @channels = Channel.find(:all, :order => "title")
    @user = current_user
  end
  
  def channel
    @first = Article.last
    @articles = Article.where("channel_id='#{params[:c_id]}'");
    @channels = Channel.find(:all, :order => "title")
    @user = current_user
    @popular_posts = Article.find(:all, :order => "created_at", :limit => 5)
    @channels = Channel.find(:all, :order => "title")
  end

  # GET /articles/1
  # GET /articles/1.xml
  def show
    begin
      @article = Article.find(params[:id])
    rescue ActiveRecord::RecordNotFound      
      return nil
    end

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @article }
    end
  end
  
  def details
    @article = Article.find(params[:id])
    # @popular = Video.find(:all, :order => "created_at")
    @channels = Channel.find(:all, :order => "title")
    @user = current_user
    @popular_posts = Article.find(:all, :order => "created_at", :limit => 5)
    @channels = Channel.find(:all, :order => "title")
  end

  # GET /articles/new
  # GET /articles/new.xml
  def new
    @article = Article.new
    @event = Event.find(params[:event_id]) if !params[:event_id].nil?
    @article_statuses = ArticleStatus.all.collect {|p| [ p.title, p.id ] }

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/1/edit
  def edit
    @article = Article.find(params[:id])
    @event = @article.event if !@article.event.nil?
    @article_statuses = ArticleStatus.all.collect {|p| [ p.title, p.id ] }
  end

  # POST /articles
  # POST /articles.xml
  def create
    
    @article = Article.new(params[:article])
    @article_statuses = ArticleStatus.all.collect {|p| [ p.title, p.id ] }
    
    # Add event
    if !params[:event_id].nil?
      @event = Event.find(params[:event_id])
      @article.event = @event
    end
    
    # Add current user
    @article.user = current_user

    respond_to do |format|
      if @article.save
        format.html {
          if params[:event_id].nil?
            redirect_to(@article, :notice => 'Article was successfully created.')
          else
            redirect_to(@event, :notice => 'Article was successfully created.')
          end
        }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /articles/1
  # PUT /articles/1.xml
  def update
    @article = Article.find(params[:id])
    
    # save article history
    @article.save_history(current_user.username)

    respond_to do |format|
      if @article.update_attributes(params[:article])
        format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def published
    @article = Article.find(params[:id])
    
    if @article.published
      @article.published = false
      @turn = "off"
      @do = "un"
      @title = "No"
    else
      @article.published = true
      @turn = "on"
      @do = ""
      @title = "Yes"
    end
    
    respond_to do |format|
      if @article.save
        format.html { redirect_to(articles_url, :notice => "Article was successfully #{@do}published.") }
        format.js { render 'published.js.erb' }
      else
        format.html { redirect_to(articles_url, :notice => "Article didn\'t been #{@do}published.") }
        format.js { render 'published_err.js.erb' }
      end
    end
      
  end  
  

  # DELETE /articles/1
  # DELETE /articles/1.xml
  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    # @page = params[:page].nil? ? 1 : params[:page]
    
    # return nil
  
    #@articles = Article.all.paginate :page=>params[:page], :order=>'created_at desc', :per_page => 3
    
    respond_to do |format|
        format.html { redirect_to(articles_url) }
        format.xml  { head :ok }
        format.js { render 'destroy.js.erb' }
    end
  end
  
  def delete_selected
    @ids = params[:ids]
    @ids.each do |obj|
      @article = Article.find(obj[0])
      @article.destroy
    end
    
    respond_to do |format|
        format.html { redirect_to(articles_url, :notice => "Articles was successfully deleted.") }
        format.xml  { head :ok }
        format.js { render 'destroy.js.erb' }
    end
  end
  
  def preview_article
    @article = Article.find(params[:id])
    render :layout => false
  end
  
end
